package view;

import entity.CourseDO;
import entity.StudentDO;
import handler.UpdateCourseViewHandler;
import handler.UpdateStudentViewHandler;
import service.CourseService;
import service.StudentService;
import service.impl.CourseServiceImpI;
import service.impl.StudentServiceImpI;

import javax.swing.*;
import java.awt.*;

public class UpdateCoursView extends JDialog {

    JPanel jPanel = new JPanel();
    //    JLabel snoLabel = new JLabel("学号");
//    JTextField snoTxt = new JTextField();
    JLabel nameLabel = new JLabel("课程名");
    JTextField nameTxt = new JTextField();

    JLabel snoLabel = new JLabel("课程号");
    JTextField snoTxt = new JTextField();

    JLabel sexLabel = new JLabel("学分");
    JTextField sexTxt = new JTextField();

    JLabel ageLabel = new JLabel("教师号");
    JTextField ageTxt = new JTextField();

//
//    JLabel deptLabel = new JLabel("系别");
//    JTextField deptTxt = new JTextField();
//
//
//    JLabel speLabel = new JLabel("专业");
//    JTextField speTxt = new JTextField();
//
//
//    JLabel classLabel = new JLabel("班级");
//    JTextField classTxt = new JTextField();

    //JButton addBtn = new JButton("添加");

    UpdateCourseViewHandler updateCourseViewHandler;
    //
    JButton updateBtn = new JButton("修改");
//
//    UpdateStudentViewHandler updateStudentViewHandler;

    public UpdateCoursView(CourseView courseView, int selectCourseId) {
        super(courseView, "修改课程信息", true);
        //设置监听
        updateCourseViewHandler = new UpdateCourseViewHandler(this, courseView);
        //查询对应记录并回显
        CourseService courseService = new CourseServiceImpI();
        CourseDO selectCourse = courseService.getById(selectCourseId);

        snoLabel.setPreferredSize(new Dimension(80, 30));
        jPanel.add(snoLabel);
        snoTxt.setPreferredSize(new Dimension(200, 30));
        snoTxt.setText(selectCourse.getCno() + "");
        //id设置为不可编辑
        snoTxt.setEnabled(false);
        jPanel.add(snoTxt);

        nameLabel.setPreferredSize(new Dimension(80, 30));
        jPanel.add(nameLabel);
        nameTxt.setPreferredSize(new Dimension(200, 30));
        nameTxt.setText(selectCourse.getCname());
        //nameTxt.setEnabled(false);
        jPanel.add(nameTxt);

        sexLabel.setPreferredSize(new Dimension(80, 30));
        jPanel.add(sexLabel);
        sexTxt.setPreferredSize(new Dimension(200, 30));
        sexTxt.setText(selectCourse.getCredits());
        //sexTxt.setEnabled(false);
        jPanel.add(sexTxt);

        ageLabel.setPreferredSize(new Dimension(80, 30));
        jPanel.add(ageLabel);
        ageTxt.setPreferredSize(new Dimension(200, 30));
        ageTxt.setText(String.valueOf(selectCourse.getTno()));
        //ageTxt.setEnabled(false);
        jPanel.add(ageTxt);

//        deptLabel.setPreferredSize(new Dimension(80,30));
//        jPanel.add(deptLabel);
//        deptTxt.setPreferredSize(new Dimension(200,30));
//        deptTxt.setText(selectStu.getDept());
//        //deptTxt.setEnabled(false);
//        jPanel.add(deptTxt);
//
//        speLabel.setPreferredSize(new Dimension(80,30));
//        jPanel.add(speLabel);
//        speTxt.setPreferredSize(new Dimension(200,30));
//        speTxt.setText(selectStu.getSpe());
//        //speTxt.setEnabled(false);
//        jPanel.add(speTxt);
//
//        classLabel.setPreferredSize(new Dimension(80,30));
//        jPanel.add(classLabel);
//        classTxt.setPreferredSize(new Dimension(200,30));
////        classTxt.setText(String.valueOf(selectStu.getClass()));//错的
//        classTxt.setText(selectStu.getSclass());
//        //classTxt.setEnabled(false);
//        jPanel.add(classTxt);

        updateBtn.addActionListener(updateCourseViewHandler);
        jPanel.add(updateBtn);

        Container contenPane = getContentPane();
        contenPane.add(jPanel);

        setSize(600, 400);//大小
        setVisible(true);//可以看见
        setResizable(false);//不可变大小
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);//退出自动关闭
        setLocationRelativeTo(null);//居中
    }

    //获取修改后的对象
    public CourseDO buildUpdateCourseDO() {
        CourseDO courseDO = new CourseDO();
        courseDO.setCno(Integer.valueOf(snoTxt.getText()));
        courseDO.setCname(nameTxt.getText());
        courseDO.setCredits(sexTxt.getText());
        courseDO.setTno(ageTxt.getText());
        return courseDO;
    }
}
